package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbUserDao extends JpaRepository<TbUser, Integer>, JpaSpecificationExecutor<TbUser> {

	TbUser getTbUserById(Integer id);


	TbUser getByUsername(String username);

	@Query("from TbUser u where u.code =:codeKey or u.username =:codeKey")
	TbUser getByCodeOrUsername(@Param("codeKey") String codeKey);

	@Query("select max(u.code) from TbUser u where u.tenantId=:tenantId")
	String findMaxCode(@Param("tenantId")Integer tenantId);


	@Query("select u.id from TbUser u where u.defaultSysId=:systemId")
	List<Integer> findAllIdByDefSystemName(@Param("systemId")Integer systemId);


	@Modifying
	@Query("update TbUser u set u.defaultSysId=:systemId, u.defaultSysName=:systemName, u.defaultSysImgUrl=:systemImageUrl where u.id in(:userIdList)")
	void updateDefSystem(@Param("userIdList")List<Integer> userIdList,@Param("systemId")Integer systemId, @Param("systemName")String systemName, @Param("systemImageUrl")String systemImageUrl);

	@Modifying
	@Query("update TbUser u set u.defaultSysName=:systemName, u.defaultSysImgUrl=:systemImageUrl where u.id in(:userIdList)")
	void updateDefSystemName(@Param("userIdList")List<Integer> userIdList, @Param("systemName")String systemName, @Param("systemImageUrl")String systemImageUrl);

	@Query("from TbUser u where u.code=:code")
	TbUser getUserByCode(@Param("code")String code);



	@Query(" from TbUser u where u.id in (:idList) and u.status='04' ")
	List<TbUser> findAllByIds(@Param("idList") List<Integer> idList);



	@Query(" from TbUser u where u.tenantId=:tenantId and u.status='04'")
	List<TbUser> findAllByTenantId(@Param("tenantId") Integer tenantId);

	@Query("from TbUser u where u.code in(:codeList)")
	List<TbUser> findUserByCodeList(@Param("codeList") List<String> codeList);

	@Query("from TbUser u where u.feishuUserId=:feishuUserId")
	TbUser getTbUserByFeiShuId(@Param("feishuUserId") String feishuUserId);

	@Query("from TbUser u where u.id not in(:idList) and u.tenantId=:tenantId and u.status='04'")
	List<TbUser> findTenantNeedCancelUser(@Param("idList") List<Integer> idList, @Param("tenantId")Integer tenantId);
}
